package com.ytc.teaching.service.impl;

import  com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ytc.commons.constants.CourseConstants;
import com.ytc.commons.model.dto.PageRequest;
import com.ytc.commons.model.dto.WorkDto;
import com.ytc.commons.model.vo.MediaVo;
import com.ytc.commons.model.vo.PageResponseResult;
import com.ytc.commons.model.vo.ResponseResult;
import com.ytc.commons.model.vo.WorkVo;
import com.ytc.commons.utils.AuthInfoHolder;
import com.ytc.commons.utils.BeanHelper;
import com.ytc.teaching.domain.Comment;

import com.ytc.teaching.domain.Work;
import com.ytc.teaching.mappers.WorkMapper;
import com.ytc.teaching.service.CommentService;
import com.ytc.teaching.service.WorkService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {

    //分页查询试卷信息
    @Override
    public ResponseResult workList(PageRequest request,WorkDto dto) {
        Integer pageNo = request.getPageNo();
        Integer pageSize = request.getPageSize();
        //分页页码参数
        Page<Work> page = new Page<>(pageNo, pageSize);
        //查询条件
        LambdaQueryWrapper<Work> wrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        wrapper.like(Work::getTitle,dto.getTitle());

        //分页查询
       // Page<Work> page1 = this.page(page,wrapper);
        Page<Work> page1 = this.page(page, wrapper);

//        List<Work> list = this.list();//page1.getRecords();
//        List<WorkVo> voList = BeanHelper.copyWithCollection(list,WorkVo.class);
        //构造返回
        return new PageResponseResult(page1.getTotal(),page1.getRecords());

    }

    //新增或修改试卷
    @Override
    public ResponseResult saveWork(WorkDto dto) {
        LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper();
        qw.eq(Work::getWorkId,dto.getWorkId());
         Work work = this.getOne(qw);
        //1.判断是否存在此数据
        if(StringUtils.isEmpty(work)) {
             work = BeanHelper.copyProperties(dto, Work.class);
            work.setStatus(1);
            work.setChangeDate(new Date());
            work.setCreateDate(new Date());
            work.setUsername(dto.getUsername());
            this.save(work);
            return ResponseResult.okResult();
        }else {
            work = BeanHelper.copyProperties(dto, Work.class);
            this.updateById(work);
            return ResponseResult.okResult();
        }
    }

    //删除试卷
    @Override
    public ResponseResult deleteByWorkId(Long workId) {
        this.removeById(workId);
        return ResponseResult.okResult();
    }
}
